package com.ys.web.user.friends.controller;

import com.ys.base.BaseConstants;
import com.ys.base.EnumType;
import com.ys.utils.annotation.GroupValidateAnnotation;
import com.ys.utils.base.http.ResponseHandler;
import com.ys.utils.base.j2se.StringHandler;
import com.ys.utils.redis.aop.RedisCache;
import com.ys.utils.spring.properties.Config;
import com.ys.web.user.friends.service.GroupSetService;
import com.ys.web.user.friends.vo.GroupSetVo;
import com.ys.web.user.friends.vo.UserGroupVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author quxinjun
 * @version 1.0.0
 * @ClassName GroupSetController.java
 * @Description 群组设置
 * @createTime 2021年05月24日 14:00:00
 */
@Slf4j
@RestController
@RequestMapping("/groupSet")
public class GroupSetController {

    @Autowired
    private GroupSetService groupSetService;

    /**
     * @param groupId 群组id
     * @title getManagerList
     * @description 获取管理员接口
     * @author quxinjun
     * @updateTime 2021/5/26 19:16
     * @return: java.lang.String
     */
    @PostMapping("/getManagerList")
    public String getManagerList(@RequestAttribute("uid") Long uid, String groupId, String searchKey, Integer type) {
        if (StringHandler.isEmpty(groupId)) {
            return ResponseHandler.error(EnumType.ResponseCode.Fail.getCode(), "参数传递出错");
        }
        if (StringHandler.isEmpty(type) || type == 0) {
            type = 2;
        }
        List<UserGroupVO> list = groupSetService.getManagerList(uid, groupId, searchKey, type);
        return ResponseHandler.list(list);
    }

    /**
     * @param uid      操作用户id
     * @param groupId  组id
     * @param memberId 成员id
     * @title setGroupManager
     * @description 设置群管理员
     * @author quxinjun
     * @updateTime 2021/5/26 14:51
     * @return: java.lang.String
     */
    @PostMapping("/setGroupManager")
    @GroupValidateAnnotation
    public String setGroupManager(@RequestAttribute Long uid, String groupId, String memberId) {
        if (StringHandler.isEmpty(groupId) || StringHandler.isEmpty(memberId)) {
            return ResponseHandler.error(EnumType.ResponseCode.Fail.getCode(), "传递参数出错");
        }
        groupSetService.setGroupManager(uid, groupId, memberId);
        return ResponseHandler.success();
    }

    /**
     * @param uid      操作人id
     * @param groupId  组id
     * @param memberId 成员id
     * @title undoGroupManager
     * @description 取消管理员
     * @author quxinjun
     * @updateTime 2021/5/26 18:35
     * @return: java.lang.String
     */
    @PostMapping("/undoGroupManager")
    @GroupValidateAnnotation
    public String undoGroupManager(@RequestAttribute Long uid, String groupId, String memberId) {
        if (StringHandler.isEmpty(groupId) || StringHandler.isEmpty(memberId)) {
            return ResponseHandler.error(EnumType.ResponseCode.Fail.getCode(), "传递参数出错");
        }
        groupSetService.undoGroupManager(uid, groupId, memberId);
        return ResponseHandler.success();
    }


    /**
     * @title getBannedList
     * @description 获取禁言选择列表
     * @author quxinjun
     * @updateTime 2021/5/27 16:08
     * @return: java.lang.String
     */
    @RedisCache
    @PostMapping("/getBannedList")
    public String getBannedList() {
        return ResponseHandler.list(groupSetService.getBannedList());
    }


    /**
     * @param uid      操作用户
     * @param memberId 成员id（多个逗号隔开）
     * @param groupId  群组id
     * @title bannedUser
     * @description 设置禁言成员
     * @author quxinjun
     * @updateTime 2021/5/27 17:20
     * @return: java.lang.String
     */
    @GroupValidateAnnotation
    @PostMapping("/setBannedUser")
    public String setBannedUser(@RequestAttribute Long uid, String groupId, String memberId, Integer code) {
        if (StringHandler.isEmpty(groupId) || StringHandler.isEmpty(memberId)) {
            return ResponseHandler.error(EnumType.ResponseCode.Fail.getCode(), "传递参数出错");
        }
        if (StringHandler.isEmpty(code) || code == 0) {
            code = 1;
        }
        groupSetService.setBannedUser(uid, groupId, memberId, code);
        return ResponseHandler.success();
    }

    /**
     * @param uid       操作用户id
     * @param groupId   群组id
     * @param searchKey 搜索关键字
     * @param type      操作类型 1：获取禁言成员 2：获取未禁言成员
     * @title getBannedMembersList
     * @description 获取可以禁言, 或禁言的成员列表
     * @author quxinjun
     * @updateTime 2021/5/27 20:47
     * @return: java.lang.String
     */
    @GroupValidateAnnotation
    @PostMapping("/getBannedMembersList")
    public String getBannedMembersList(@RequestAttribute Long uid, String groupId, Integer type, String searchKey) {
        if (StringHandler.isEmpty(groupId)) {
            return ResponseHandler.error(EnumType.ResponseCode.Fail.getCode(), "传递参数出错");
        }
        if (StringHandler.isEmpty(type)) {
            type = 1;
        }
        List<UserGroupVO> list = groupSetService.getBannedMembersList(uid, groupId, type, searchKey);
        return ResponseHandler.list(list);
    }

    /**
     * @param uid      操作人id
     * @param groupId  群组id
     * @param memberId 成员id（多个逗号隔开）
     * @title cancelBanned
     * @description 取消成员禁言
     * @author quxinjun
     * @updateTime 2021/5/28 9:48
     * @return: java.lang.String
     */
    @GroupValidateAnnotation
    @PostMapping("/cancelBanned")
    public String cancelBanned(@RequestAttribute Long uid, String groupId, String memberId) {
        if (StringHandler.isEmpty(groupId) || StringHandler.isEmpty(memberId)) {
            return ResponseHandler.error(EnumType.ResponseCode.Fail.getCode(), "传递参数出错");
        }
        groupSetService.cancelBanned(uid, groupId, memberId);
        return ResponseHandler.success();
    }


    /**
     * @param uid     操作用户
     * @param groupId 群组id
     * @title getCurrentGroupSetForUser
     * @description 获取用户在当前组的一些配置
     * @author quxinjun
     * @updateTime 2021/6/1 14:21
     * @return: java.lang.String
     */
    @GroupValidateAnnotation
    @PostMapping("/getCurrentGroupSetForUser")
    public String getCurrentGroupSetForUser(@RequestAttribute Long uid, String groupId) {
        if (StringHandler.isEmpty(groupId)) {
            return ResponseHandler.error(EnumType.ResponseCode.Fail.getCode(), "传递参数出错");
        }
        GroupSetVo groupSetVo = groupSetService.getCurrentGroupSetForUser(uid, groupId);
        return ResponseHandler.success(groupSetVo);
    }

    /**
     * @title getGroupMessageClearSetList
     * @description 获取定时清理消息设置的集合
     * @author quxinjun
     * @updateTime 2021/6/4 16:19
     * @return: java.lang.String
     */
    @PostMapping("/getGroupMessageClearSetList")
    public String getGroupMessageClearSetList() {
        return ResponseHandler.list(groupSetService.getGroupMessageClearSetList());
    }

    /**
     * @param uid     截屏用户id
     * @param groupId 群组id
     * @param url     图片路径
     * @title screensHotsNotice
     * @description 截屏通知
     * @author quxinjun
     * @updateTime 2021/6/7 12:11
     * @return: java.lang.String
     */
    @PostMapping("/screensHotsNotice")
    public String screensHotsNotice(@RequestAttribute Long uid, String groupId, String url) {
        if (StringHandler.isEmpty(groupId) || StringHandler.isEmpty(url)) {
            return ResponseHandler.error(EnumType.ResponseCode.Fail.getCode(), "传递参数出错");
        }
        groupSetService.screensHotsNotice(uid, groupId, url);
        return ResponseHandler.success();
    }

    /**
     * @param groupId 群组id
     * @title getCurrentGroupScreenList
     * @description 获取群中用户截屏信息
     * @author quxinjun
     * @updateTime 2021/6/8 15:53
     * @return: java.lang.String
     */
    @PostMapping("/getCurrentGroupScreenList")
    public String getCurrentGroupScreenList(@RequestAttribute Long uid, String groupId) {
        if (StringHandler.isEmpty(groupId)) {
            return ResponseHandler.error(EnumType.ResponseCode.Fail.getCode(), "传递参数出错");
        }
        return ResponseHandler.list(groupSetService.getCurrentGroupScreenList(uid, groupId));
    }


    /**
     * @param uid      操作人id
     * @param groupId  群组id
     * @param memberId 成员id，多个逗号隔开
     * @title setBanGetRedPacket
     * @description 设置禁止领取红包
     * @author quxinjun
     * @updateTime 2021/7/5 10:40
     * @return: java.lang.String
     */
    @GroupValidateAnnotation
    @PostMapping("/setBanGetRedPacket")
    public String setBanGetRedPacket(@RequestAttribute Long uid, String groupId, String memberId) {
        if (StringHandler.isEmpty(groupId) || StringHandler.isEmpty(memberId)) {
            return ResponseHandler.error(EnumType.ResponseCode.Fail.getCode(), "传递参数出错");
        }
        groupSetService.setBanGetRedPacket(uid, groupId, memberId);
        return ResponseHandler.success();
    }


    /**
     * @param uid      操作人id
     * @param groupId  群id
     * @param memberId 成员id，多个逗号隔开
     * @title cancelBanGetRedPacket
     * @description 取消禁止抢红包
     * @author quxinjun
     * @updateTime 2021/7/5 12:41
     * @return: java.lang.String
     */
    @GroupValidateAnnotation
    @PostMapping("/cancelBanGetRedPacket")
    public String cancelBanGetRedPacket(@RequestAttribute Long uid, String groupId, String memberId) {
        if (StringHandler.isEmpty(groupId) || StringHandler.isEmpty(memberId)) {
            return ResponseHandler.error(EnumType.ResponseCode.Fail.getCode(), "传递参数出错");
        }
        groupSetService.cancelBanGetRedPacket(uid, groupId, memberId);
        return ResponseHandler.success();
    }

    /**
     * @param uid       操作人id
     * @param groupId   群id
     * @param type      操作类型 1：获取禁止抢红包成员 2：获取未禁止抢红包成员
     * @param searchKey 搜索关键字
     * @title getBanGradMembersList
     * @description 获取可以被禁止抢红包或者，禁止抢红包的成员信息
     * @author quxinjun
     * @updateTime 2021/7/5 12:52
     * @return: java.lang.String
     */
    @GroupValidateAnnotation
    @PostMapping("/getBanGradMembersList")
    public String getBanGradMembersList(@RequestAttribute Long uid, String groupId, Integer type, String searchKey) {
        if (StringHandler.isEmpty(groupId)) {
            return ResponseHandler.error(EnumType.ResponseCode.Fail.getCode(), "传递参数出错");
        }
        if (StringHandler.isEmpty(type)) {
            type = 1;
        }
        List<UserGroupVO> list = groupSetService.getBanGradMembersList(uid, groupId, type, searchKey);
        return ResponseHandler.list(list);
    }


    /**
     * @param uid     操作人id
     * @param groupId 群id
     * @title clearMessageRightAway
     * @description 立刻清理消息
     * @author quxinjun
     * @updateTime 2021/7/28 10:20
     * @return: java.lang.String
     */
    @GroupValidateAnnotation
    @PostMapping("/clearMessageRightAway")
    public String clearMessageRightAway(@RequestAttribute Long uid, String groupId) {
        if (StringHandler.isEmpty(groupId)) {
            return ResponseHandler.error(EnumType.ResponseCode.Fail.getCode(), "传递参数出错");
        }
        groupSetService.clearMessageRightAway(uid, groupId);
        return ResponseHandler.success();
    }

    /**
     * @throws
     * @title setGradInfo
     * @description 获取用户抢红包设置信息
     * @author quxinjun
     * @updateTime 2021/12/14 16:05
     * @return: java.lang.String
     */
    @PostMapping("/setGradInfo")
    public String setGradInfo() {
        String timeStr = Config.pp.get(BaseConstants.DataSourceConstants.RED_PACKAGE_GRAD_TIME);
        String countStr = Config.pp.get(BaseConstants.DataSourceConstants.RED_PACKAGE_GRAD_COUNT);
        if (timeStr == null || countStr == null) {
            return ResponseHandler.error("500", "获取配置失败，请联系客服");
        }
        Map<String, Object> result = new HashMap<>();
        result.put("time", timeStr.split(","));
        result.put("count", countStr.split(","));
        return ResponseHandler.success(result);
    }

    /**
     * @param
     * @throws
     * @title setGradTimeForGroup
     * @description
     * @author quxinjun
     * @updateTime 2021/12/14 16:54
     * @return: java.lang.String
     */
    @GroupValidateAnnotation
    @PostMapping("/setGradTimeForGroup")
    public String setGradTimeForGroup(@RequestAttribute Long uid, String groupId, Integer count, Integer time) {
        if (StringHandler.isEmpty(groupId) || StringHandler.isEmpty(count) || StringHandler.isEmpty(time)) {
            return ResponseHandler.error("500", "参数传递错误");
        }
        groupSetService.setGradTimeForGroup(uid, groupId, count, time);
        return ResponseHandler.success();
    }

}
